/*	Title: r3testerA.c
	Author: Aaron Daniels
	Description: File to test the functionality of R3 by calling its various component functions.
	Last updated: 10/25/11
*/

#include "3testA.h"

/* REFERENCE
int main(){
	sys_init(MODULE_R4); //initialize system, defined in mpx_supt.h
	sys_set_vec(sys_call);
	init_stack_and_rq(); //defined in r3code.h, initialize stack and ready queue 
	comhan();
	sys_exit(); //tear down, defined in mpx_supt.h
	return 0;
}*/

int main(){
	testInit();
	if (testR3())	{
		printf("All tests were performed successfully. Now exiting.");
	}
	else	{
		printf("Tests failed. Now exiting.");
	}
	sys_exit();
	return 0;
}

void testInit(){
	sys_init(MODULE_R4);
	sys_set_vec(sys_call);
	init_stack_and_rq();
}

int testR3(){	
	//First complete runthrough of all processes.	
	basicTest();	
	testPrint(1);
	
	//Second complete runthrough of all processes.
	basicTest();
	testPrint(2);
	
	//Testing the blocking of one process.
	load_procs();
	testBlock(5);
	dispatch();
	testUnblock(5);
	dispatch();
	testPrint(3);
	
	//Testing the blocking of two processes.
	load_procs();
	testBlock(4);
	testBlock(5);
	dispatch();
	testUnblock(4);
	testUnblock(5);
	dispatch();
	testPrint(4);
	
	//Testing the suspension of one process.
	load_procs();
	testSuspend(5);
	dispatch();
	testResume(5);
	dispatch();
	testPrint(5);
	
	//Testing the suspension of two processes.
	load_procs();
	testSuspend(4);
	testSuspend(5);
	dispatch();
	testResume(4);
	testResume(5);
	dispatch();
	testPrint(6);
	
	//Testing the blocking and suspension of one process.
	load_procs();
	testBlock(5);
	testSuspend(5);
	dispatch();
	testUnblock(5);
	testResume(5);
	dispatch();
	testPrint(7);
	
	return 1;
}

void basicTest(){
	load_procs();
	dispatch();
}

void testPrint(int testNumber){
	printf("Test %d completed.\n", testNumber);
}

void testBlock(int testNumber){	
	block_pcb(nameBuilder(testNumber));
}

void testUnblock(int testNumber){	
	unblock_pcb(nameBuilder(testNumber));
}

void testSuspend(int testNumber){
	suspend_pcb(nameBuilder(testNumber));
}

void testResume(int testNumber){
	resume_pcb(nameBuilder(testNumber));
}

char* nameBuilder(int testNumber){
	char* nameString;
	sprintf(nameString, "TEST%d", testNumber);
	return nameString;
}